/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.suai.manager;

import java.util.HashMap;
import java.util.Map;
import org.suai.dao.impl.SystemStatistic;
import org.suai.system.QueueSystem;

/**
 *
 * @author awm
 */
public final class StatisticManager {
    private int countCreatedPackage;
    private int countServicePackage;
    private int countDeletedPackage;

    private static final StatisticManager instance = new StatisticManager();

    private Map systemStatistic;

    public static StatisticManager getInstance() {
        return instance;
    }

    public void restoreStats(){
        countCreatedPackage = 0;
        countDeletedPackage = 0;
        countServicePackage = 0;
        systemStatistic = new HashMap();
    }

    public void createPackage(QueueSystem system) {
        SystemStatistic item = null;
        if( systemStatistic.containsKey(system) ) {
            item = (SystemStatistic)systemStatistic.get(system);
        } else {
            item = new SystemStatistic();
            
        }
        item.createPackage();
        systemStatistic.put(system, item);
        countCreatedPackage++;
    }

    public void servicePackage(QueueSystem system) {
        SystemStatistic item = null;
        if( systemStatistic.containsKey(system) ) {
            item = (SystemStatistic)systemStatistic.get(system);
            item.servicePackage();
        } else {
            item = new SystemStatistic();
            item.servicePackage();
        }
        systemStatistic.put(system, item);
        countServicePackage++;
    }

    public void deletePackage(QueueSystem system) {
        SystemStatistic item = null;
        if( systemStatistic.containsKey(system) ) {
            item = (SystemStatistic)systemStatistic.get(system);
        } else {
            item = new SystemStatistic();
        }

        item.deletePackage();
        systemStatistic.put(system, item);
        countDeletedPackage++;
    }

    public String toString() {
        StringBuffer result = new StringBuffer("\nStatistics:\n")
                .append("\nCreate packages: ").append(countCreatedPackage)
                .append("\nService packages: ").append(countServicePackage)
                .append("\nDeleted packages: ").append(countDeletedPackage);

        for(Object key : systemStatistic.keySet().toArray()) {
            SystemStatistic stat = (SystemStatistic)systemStatistic.get(key);
            result.append("\n\nSystem #").append(((QueueSystem)key).getSystemNumber());
            result.append(stat);
        }
        return result.toString();
    }

}
